import GameConfig from "./GameConfig";
import CustomValue2D from "./script/shader/CustomValue2D";
import { MyMeshQuadTexture } from "./script/shader/MyMeshQuadTexture";
import { MyShaderSprite } from "./script/shader/MyShaderSprite";
import { testShaderSprite } from "./script/testShaderSprite";
class Main {
    constructor() {
        //初始化引擎
        Laya.init(GameConfig.width, GameConfig.height, Laya.WebGL);
        Laya.stage.bgColor = "#cfcfcf";
        Laya.stage.scaleMode = GameConfig.scaleMode;
        Laya.stage.screenMode = GameConfig.screenMode;
        Laya.stage.alignV = GameConfig.alignV;
        Laya.stage.alignH = GameConfig.alignH;
        Laya.Stat.show();
        //加载一个图片
        Laya.loader.load("res/atlas/comp.atlas", Laya.Handler.create(this, this.loadComplete), null, Laya.Loader.ATLAS);
    }

    private loadComplete(): void {
        let texture1: Laya.Texture = Laya.Loader.getRes("comp/image.png");
        let texture2: Laya.Texture = Laya.Loader.getRes("comp/button.png");
        // 创建一个shadervalue，shadervalue相同的会合批

        // 这里是一个简单的圆形裁切shader
        let shaderValue = new CustomValue2D(`
        attribute vec4 posuv;
        attribute vec2 uvCenter;
        uniform vec2 size;	
        varying vec2 uv;
        varying float cx;
        varying float cy;
        void main() {
            vec4 pos = vec4(posuv.xy,0.,1.);
            vec4 pos1  =vec4((pos.x/size.x-0.5)*2.0,(0.5-pos.y/size.y)*2.0,0.,1.0);
            gl_Position=pos1;
            uv = posuv.zw;
            cx = uvCenter.x;
            cy = uvCenter.y;
        }`,
            `#if defined(GL_FRAGMENT_PRECISION_HIGH)
        precision highp float;
        #else
        precision mediump float;
        #endif
        varying vec2 uv;
        uniform sampler2D texture;
        uniform vec2 size;
        varying float cx;
        varying float cy;
        const float uvRadius = 0.2;
        void main() {
            vec4 color= texture2D(texture, uv);
            float du = uv.x - cx;
            float dv = uv.y - cy;
            if (du * du + dv * dv < uvRadius * uvRadius) {
                gl_FragColor=color;
            }
        }`
        );

        for (let i = 0; i < 10; i++) {
            let spe: testShaderSprite = new testShaderSprite(shaderValue);
            spe.renderTexture = texture1;
            spe.pos(50 * i + 50, i * 50 + 150);
            Laya.stage.addChild(spe);

            let spe2 = new testShaderSprite(shaderValue);
            spe2.renderTexture = texture2;
            spe2.pos(50 * i + 50, (10 - i) * 50 + 150);
            Laya.stage.addChild(spe2);

            let sp = new Laya.Sprite();
            sp.texture = texture1;
            sp.pos(50 * i + 50, i * 50 + 250);
            sp.alpha = 0.5;
            Laya.stage.addChild(sp);
        }
    }

}
//激活启动类
new Main();
